#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "tsp_cipher.h"
using namespace std;

int tsp_cipher_aes_test() {
    CCipher c;
	int i;
	DataBlock key, sourceData, resultData;
	int length = 16;
	
	printf ("\n-----------------tsp generateAESKey128-----------------\n");
    c.generateAESKey128(&key);
	printf ("the key is:\n");
	printf ("key size: %d\n", key.dataLength);
	for(i = 0; i < key.dataLength; i++)
	    printf("%02x ", *(key.data + i));
	printf("\n");
	
	printf ("\n-----------------tsp AESEncipher128-----------------\n");
	sourceData.dataLength = 16;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
	for (i=0; i<sourceData.dataLength; i++){
		*(sourceData.data + i) = (UINT8)i;
	}
	c.AESEncipher128(&key, &sourceData, &resultData);
	printf("AESEncipher128 resultData:\n");
	for (i=0; i<resultData.dataLength; i++){
		printf ("%02x ", *(resultData.data + i));
	}
	printf("\n");
	
	printf ("\n-----------------tsp AESDecipher128-----------------\n");
	c.AESDecipher128(&key, &resultData, &sourceData);
	printf("AESDecipher128 resultData:\n");
	for (i=0; i<sourceData.dataLength; i++){
		printf ("%02x ", *(sourceData.data + i));
	}
	
    return 0;
}

int tsp_cipher_sha1_test() {
	CCipher c;
	DataBlock sourceData, resultData;
	int length = 16;
	int i;
	
	printf ("\n-----------------tsp doSHA1--------------------------\n");
	sourceData.dataLength = 16;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
	for (i=0; i<sourceData.dataLength; i++){
		*(sourceData.data + i) = (UINT8)i;
	}
	
	c.doSHA1(&sourceData, &resultData);
	
	printf("tsp_cipher_sha1_test resultData:\n");
	for (i=0; i<resultData.dataLength; i++){
		printf ("%02x ", *(resultData.data + i));
	}
	printf ("\n");
	
}

int tsp_cipher_rsa_test() {
    CCipher c;
	int i;
	DataBlock key, sourceData, resultData;
	int length = 16;
	
	printf ("\n-----------------tsp RSAKeyGen-----------------\n");
    c.RSAKeyGen(&key);
	printf ("the key is:\n");
	printf ("key size: %d\n", key.dataLength);
	for(i = 0; i < key.dataLength; i++)
	    printf("%02x ", *(key.data + i));
	printf("\n");
	
	printf ("\n-----------------tsp RSAEncipher-----------------\n");
	sourceData.dataLength = 16;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
	for (i=0; i<sourceData.dataLength; i++){
		*(sourceData.data + i) = (UINT8)i;
	}
	c.RSAEncipher(&key, &sourceData, &resultData);
	printf("RSAEncipher resultData:\n");
	for (i=0; i<resultData.dataLength; i++){
		printf ("%02x ", *(resultData.data + i));
	}
	printf("\n");
	
	printf ("\n-----------------tsp RSADecipher-----------------\n");
	c.RSADecipher(&key, &resultData, &sourceData);
	printf("RSADecipher resultData:\n");
	for (i=0; i<sourceData.dataLength; i++){
		printf ("%02x ", *(sourceData.data + i));
	}
	
    return 0;
}

int tsp_cipher_pcr_test(){
	DataBlock sourceData, resultData;
	CCipher c;
	int i;
	
	//写PCR
	printf("\n-------------------tsp PCRWrite---------------------\n");
	sourceData.dataLength = 20;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
	
	c.PCRWrite(0, &sourceData);
	
	//读PCR
	printf("\n-------------------tsp PCRRead---------------------\n");
	c.PCRRead(0, &resultData);
	printf ("pcr resultData is :\n");
	for (i=0; i<resultData.dataLength; i++){
		printf("%02x ", *(resultData.data + i));
	}
	printf ("\n");
}


void tsp_cipher_random_test() {
	DataBlock resultData;
	CCipher c;
	int i;
	
	//获取随机数
	printf("\n-------------------tsp getRandom---------------------\n");
	c.getRandom(8, &resultData);
	printf ("pcr resultData is :\n");
	for (i=0; i<resultData.dataLength; i++){
		printf("%02x ", *(resultData.data + i));
	}
	printf ("\n");
	
}

int tsp_cipher_test() {
	tsp_cipher_aes_test();
	tsp_cipher_sha1_test();
	tsp_cipher_pcr_test();
	tsp_cipher_random_test();
	tsp_cipher_rsa_test();
	
}
